<template>
	<view class="box">

		<my-goods @click.native="toDetail(item.goods_id)" :item='item' v-for="(item,index) in dataList" :key='index'>
		</my-goods>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: [],
				key: '',
				pagesize: 10,
				total: '',
				pagenum: 1,
				loading: false
			};
		},

		onLoad(options) {
			uni.setNavigationBarTitle({
				title: options.key
			})
			this.key = options.key
			this.getDateList()
		},
		// 上拉加载数据
		onReachBottom() {
			if (this.loading) return
			this.pagenum += 1
			this.getDateList()
		},
		// 下拉刷新
		onPullDownRefresh() {
			console.log(111);
			this.pagenum = 1
			this.dataList = []
			this.total = -1
			this.loading = false
			this.getDateList(true)
		},
		methods: {
			async getDateList(bool) {
				if (this.dataList.length === this.total) {
					return uni.showToast({
						title: '没得了哦吊毛'
					})
				}
				this.loading = true
				const res = await uni.$http.get(
					`/api/public/v1/goods/search?query=${this.key}&pagenum=${this.pagenum}&pagesize=${this.pagesize}`
				)
				// console.log(res);
				this.dataList = [...this.dataList, ...res.data.message.goods]
				this.total = res.data.message.total
				this.loading = false
				if (this.total === 0) {
					return uni.showToast({
						title: '毛都没有'
					})
				}
				if (bool) {
					uni.stopPullDownRefresh()
				}
			},
			toDetail(id) {
				uni.navigateTo({
					url: `/pages/goods_detail/main?goods_id=${id}`
				})
			}
		}
	}
</script>

<style lang="scss">

</style>
